package com.LeeCode;

/**
 * 找到最长的半重复子字符串
 */

public class Code2730 {
    public static void main(String[] args) {
        String s = "1";
        System.out.println(new Code2730().longestSemiRepetitiveSubstring(s));
    }

    public int longestSemiRepetitiveSubstring(String s) {
        int ans = 1, left = 0, cnt = 0;

        char[] chars = s.toCharArray();

        for (int i = 1; i < chars.length; i++) {
            if (chars[i] == chars[i - 1])
                cnt++;

            while (cnt == 2) {
                if (chars[left] == chars[left + 1]) {
                    cnt--;
                }
                left++;
            }

            ans = Math.max(ans, i - left + 1);
        }
        return ans;
    }
}
